package org.hxzon.blogbackup.baidu;

import java.util.ArrayList;
import java.util.List;

import org.hxzon.util.FileNameUtil;
import org.jsoup.nodes.Element;

public class BlogFile {
    private Element title;
    private Element date;
    private Element category;
    private Element content;
    private List<BlogImage> images;
    private String totalContent;
    private String origUrl;
    private int pageIndex;

    public String toString() {
        return this.title.text() + "," + this.date.text() + "," + this.category.text() + "," + this.pageIndex;
    }

    public String getFileName() {
        return FileNameUtil.windowsFileName(title.text()) + ".html";
    }

    public String getShortFileName() {
        String fileName = getFileName();
        if (fileName.length() >= 20) {
            fileName = fileName.substring(0, 20);
        }
        return fileName;
    }

    public String getCategoryText() {
        return category.text().replaceFirst("类别：", "");//.substring(3);
    }

    public String getCategoryHtml() {
//		return category.html().contains("类别：") ? category.html().substring(3) : category.text();
        return category.html().replaceFirst("类别：", "");//.substring(3);
    }

    public String getFullPath() {
        return getCategoryText() + "/" + getFileName();
    }

    public String getTotalContent() {
        if (totalContent == null) {
            StringBuilder sb = new StringBuilder();
            sb.append("<html>");
            sb.append("<meta http-equiv='content-type' content='text/html; charset=UTF-8'>");
            sb.append("<head><title>");
            sb.append(title.html());
            sb.append("</title></head><body>");
            sb.append("<div id='tit'>").append(title.html()).append("</div>");
            sb.append("<div id='cate'>").append(getCategoryHtml()).append("</div>");
            sb.append("<div id='date'>").append(date.html()).append("</div>");
            sb.append("<div id='page'>").append(pageIndex).append("</div>");
            sb.append("<a id='url' href='").append(origUrl).append("'>").append(origUrl).append("</a>");
            sb.append("<div id='cnt'>").append(content.html()).append("</div>");
            sb.append("</body></html>");
            totalContent = sb.toString();
        }
        return totalContent;
    }

    public Element getTitle() {
        return title;
    }

    public void setTitle(Element title) {
        this.title = title;
    }

    public Element getDate() {
        return date;
    }

    public void setDate(Element date) {
        this.date = date;
    }

//	public Element getCategory() {
//		return category;
//	}

    public void setCategory(Element category) {
        this.category = category;
    }

    public Element getContent() {
        return content;
    }

    public void setContent(Element content) {
        this.content = content;
    }

    public List<BlogImage> getImages() {
        return images;
    }

    public void setImages(List<BlogImage> images) {
        this.images = images;
    }

    public void addImage(BlogImage image) {
        if (this.images == null) {
            images = new ArrayList<BlogImage>();
        }
        images.add(image);
    }

    public String getOrigUrl() {
        return origUrl;
    }

    public void setOrigUrl(String origUrl) {
        this.origUrl = origUrl;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

}
